#!/usr/local/bin/python
# -*- coding: utf-8 -*-

import jieba
from PIL import Image
import numpy as np

from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator

import logging
jieba.setLogLevel(logging.INFO)

def text2wc(text, mask_image=None, stopwords=STOPWORDS):
    words = " ".join(jieba.cut(text))
    return wc(words, mask_image, stopwords)

make_wordcould = text2wc

def wc(words, mask_image=None, stopwords=STOPWORDS):  
    if mask_image:
        backgroud_image = np.array(Image.open(mask_image))
        image_colors = ImageColorGenerator(backgroud_image)
        wordcloud = WordCloud(background_color="white",
                         max_font_size=50, random_state=42,
                         max_words=2000,            # 设置最大现实的字数
                         mask=backgroud_image,        # 设置背景图片
                         stopwords=stopwords,        # 设置停用词
                         font_path='/System/Library/Fonts/PingFang.ttc').generate(words)
        wordcloud.recolor(color_func=image_colors)
    else:
        wordcloud = WordCloud(background_color="white",
                         max_font_size=50, random_state=42,
                         max_words=2000,            # 设置最大现实的字数
                         stopwords=stopwords,        # 设置停用词
                         font_path='/System/Library/Fonts/PingFang.ttc').generate(words)
    return wordcloud
